from common.exceptions import AuthorizationFailed
from util.db import Cursor
from flask import session


class TeamSpaceModel:

    def __init__(self, team_id):
        self.team_id = team_id

    @property
    def team_info(self):
        sql = """select * from `yugong_team` where `id`=%s"""
        with Cursor() as cursor:
            cursor.execute(sql, [self.team_id])
            res = cursor.fetchall()[0]
        return res

    @property
    def name(self):
        return self.team_info.get('name')

    @property
    def auth(self):
        return self.team_info.get('auth')

    @staticmethod
    def get_team_role(id, id_type):
        """获取用户在团队空间中的角色
            id: 资源ID，可以通过文章ID、知识库ID、团队ID等获取角色
            id_type：类型标识 team  book article
            鉴权： 是否在此团队内，在的话，是否有编辑权限，有的话，资源是私密的吗？是私密的话，自己的可修改，他人的不可修改
        """
        user_id = session.get('user_id')
        with Cursor() as cursor:
            if id_type == 'team':
                sql = """select role from yugong_role_team where user_id=%s and team_id in (select id from yugong_team where id=%s and (auth!=1 or user_id=%s))"""
                cursor.execute(sql, [user_id, id, user_id])
            if id_type == 'book':
                sql = """select role from yugong_role_team where user_id=%s and team_id in (select team_id from yugong_book where id=%s and (auth!=1 or user_id=%s))"""
                cursor.execute(sql, [user_id, id, user_id])

            if id_type == 'article':
                sql = """select role from yugong_role_team where user_id=%s and team_id in (select team_id from yugong_article where id=%s and (auth!=1 or create_person=%s))"""
                cursor.execute(sql, [user_id, id, user_id])
            data = cursor.fetchall()
        if data:
            return data[0]['role']
        return None

    @staticmethod
    def get_user_team_space():
        with Cursor() as cursor:
            user_id = session.get('user_id')
            sql = """select * from yugong_team where id in (select team_id from yugong_role_team where user_id=%s)"""
            cursor.execute(sql, [user_id])
            data = cursor.fetchall()
        return data

    @property
    def is_public(self) -> bool:
        with Cursor() as cursor:
            sql = """select auth from yugong_team where id=%s"""
            cursor.execute(sql, [self.team_id])
            data = cursor.fetchall()
            if data[0]['auth'] == 0:
                return True
            return False

    @property
    def is_private(self) -> bool:
        with Cursor() as cursor:
            sql = """select auth from yugong_team where id=%s"""
            cursor.execute(sql, [self.team_id])
            data = cursor.fetchall()
            if data[0]['auth'] == 1:
                return True
            return False

    @property
    def is_protected(self) -> bool:
        with Cursor() as cursor:
            sql = """select auth from yugong_team where id=%s"""
            cursor.execute(sql, [self.team_id])
            data = cursor.fetchall()
            if data[0]['auth'] == 2:
                return True
            return False
